package yur.wedding.modules.sys.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.multipart.MultipartFile;
import yur.wedding.common.annotation.SysLog;
import yur.wedding.common.utils.Constant;
import yur.wedding.modules.sys.entity.SysUserEntity;
import yur.wedding.modules.sys.service.SysUserRoleService;
import yur.wedding.modules.sys.service.SysUserService;
import yur.wedding.modules.sys.shiro.ShiroUtils;
import yur.wedding.utils.PageUtils;
import yur.wedding.utils.R;
import yur.wedding.validator.ValidatorUtils;


/**
 * 系统用户
 *
 * @author Yur
 * @email ${email}
 * @date 2021-11-09 21:15:10
 */
@RestController
@RequestMapping("sys/sysuser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;


    @Autowired
    private SysUserRoleService sysUserRoleService;


    /**
     * 查询所有用户   并且查出  部门名称
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysuser:list")
    public R list(@RequestParam Map<String, Object> params) {
        System.out.println(params.get("pageSize") + "---" + params.get("currPage"));
        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{user_id}")
    @RequiresPermissions("sys:sysuser:info")
    public R info(@PathVariable("user_id") Long user_id) {

        SysUserEntity sysUser = sysUserService.getById(user_id);

        sysUser.setPassword(sysUser.getPassword());
        //查询用户角色
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(user_id);
        sysUser.setRoleIdList(roleIdList);

        return R.ok().put("user", sysUser);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysuser:save")
    public R save(@RequestBody SysUserEntity sysUser) {

        //  校验不通过，则报RRException异常
        ValidatorUtils.validateEntity(sysUser);

        boolean b = sysUserService.saveUser(sysUser);
        if (b){
            return R.ok();
        }else {
            return R.error("编号重复");
        }



    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysuser:update")
    public R update(@RequestBody SysUserEntity sysUser) {
        ValidatorUtils.validateEntity(sysUser);
        sysUserService.updateUser(sysUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysuser:delete")
    public R delete(Long userIds) {

        if (userIds == Constant.SUPER_ADMIN) {
            return R.error("超管理员无法删除");
        }
        sysUserService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }


    @RequestMapping("/addUserList")
    @RequiresPermissions("sys:sysuser:addUserList")
    public R addUserList(@RequestParam("file") MultipartFile file) throws IOException {


        if (file.isEmpty()) {

            return R.error("文件夹为空，重新上传");
        } else {

            String fileName = file.getOriginalFilename();

            InputStream is = file.getInputStream();

            String name = fileName.substring(fileName.lastIndexOf("."));
            if (".xls".equals(name)) {
                R r = sysUserService.addUserList(fileName, is);
              return r;
            } else {
                return R.error("格式非.xls,数据添加失败");
            }

        }
    }

}
